package practise;

import practise.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo03_Insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 获取连接对象
            connection = DBUtils.getConnection();

            // 获取预处理
            String sql = "insert into student (sn, name, mail, class_id) values (?,?,?,?)";
            statement = connection.prepareStatement(sql);

            Scanner in = new Scanner(System.in);
            // 输入数据
            System.out.println("请输入学号");
            String sn = in.next();
            System.out.println("请输入姓名");
            String name = in.next();
            System.out.println("请输入邮箱");
            String mail = in.next();
            System.out.println("请输入班级Id");
            long classId = Long.parseLong(in.next());
            statement.setString(1, sn);
            statement.setString(2, name);
            statement.setString(3, mail);
            statement.setLong(4, classId);
            // 执行
            int row = statement.executeUpdate();
            if (row == 1) {
                System.out.println("插入成功");
            }
        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            // 关闭
            DBUtils.close(null, statement, connection);
        }
    }
}
